package main

import (
	"fmt"
)

func reverse(nums []int) {
	n := len(nums)
	for i := 0; i < n/2; i++ {
		nums[i], nums[n-1-i] = nums[n-1-i], nums[i]
	}
}

func getDigits(n int) []int {
	ds := []int{}
	for n != 0 {
		d := n % 10
		n = n / 10
		ds = append(ds, d)
	}
	reverse(ds)
	return ds
}

func sum(nums []int) int {
	s := 0
	for _, num := range nums {
		s += num * num
	}
	return s
}

func isHappy(n int) bool {
	results := map[int]int{}
	for {
		if n == 1 {
			return true
		} else if _, ok := results[n]; ok {
			return false
		}
		results[n] = 0
		n = sum(getDigits(n))
	}
}

func main() {
	fmt.Println(isHappy(1))
	fmt.Println(isHappy(19))
	fmt.Println(isHappy(2))
	//fmt.Println(isHappy(1))
}
